import { message, notification } from 'antd';
import { capitalize } from 'lodash';

import { isMobile } from '@/hooks/useScreenSize';
import i18n from '@/locales/i18n';

interface MessageConfig {
  content: string;
  duration?: number;
  onClose?: () => void;
}
type Actions = 'success' | 'error' | 'info' | 'warning';

const actions = ['success', 'error', 'info', 'warning'];
export const messageBox: any = {};

// 响应式的消息提示
for (let action of actions) {
  messageBox[action] = (config: MessageConfig) => {
    const { content, duration, onClose } = config;
    if (isMobile()) {
      message[action as Actions]({
        content,
        duration: duration ? duration : 5,
        onClose,
      });
    } else {
      notification[action as Actions]({
        placement: 'bottomRight',
        duration: duration ? duration : 5,
        message: capitalize(i18n.t(action)!),
        description: content,
      });
    }
  };
}

export default messageBox;
